<?php
//部门
namespace app\crm\controller;

use app\crm\model\Sys;
use think\Config;
use think\Db;
use think\Request;
use think\Exception;
use app\crm\model\Department as DepartmentModel;
use app\crm\model\Staff as StaffModel;
use app\crm\model\Cusarea as CusareaModel;
class Community extends \CrmController
{
    static private $DiDuiBumen = [];
    public function base64EncodeImage ($image_file) {
        $base64_image = '';
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
        return $base64_image;
    }
    public function testdownload(Request $request){
       
            $url = $request->get('url');
            if(!empty($url)){
              $url=  ROOT_PATH."public".DS."accessory".DS."20190613".DS."41c49caf9f021d8cfa0566885c5ec3a3.jpg";
              return $this->base64EncodeImage($url);

            }
        
    }
    public function paixu($key,$arr){
        for($i=0;$i<count($arr)-1;$i++){
            for($j=$i+1;$j<count($arr);$j++){
                if($arr[$j][$key]<$arr[$i][$key]){
                    $tmp=$arr[$j];
                    $arr[$j]=$arr[$i];
                    $arr[$i]=$tmp;
                }
            }
        }
        return $arr;
    }
    public function hebingarr($arr1,$arr2,$arr3=[],$arr4=[],$arr5=[]){
        $indexarr=[];
        $data=[];
        foreach ($arr1 as $k => $v) {
             # code...
            if(!in_array($v["index"],$indexarr)){
                $indexarr[]=$v["index"];
                $data[]=$v;
            } 
        }
        foreach ($arr2 as $k => $v) {
             # code...
            if(!in_array($v["index"],$indexarr)){
                $indexarr[]=$v["index"];
                $data[]=$v;
            } 
        } 
        foreach ($arr3 as $k => $v) {
             # code...
            if(!in_array($v["index"],$indexarr)){
                $indexarr[]=$v["index"];
                $data[]=$v;
            } 
        }
        foreach ($arr4 as $k => $v) {
             # code...
            if(!in_array($v["index"],$indexarr)){
                $indexarr[]=$v["index"];
                $data[]=$v;
            } 
        }
        foreach ($arr5 as $k => $v) {
             # code...
            if(!in_array($v["index"],$indexarr)){
                $indexarr[]=$v["index"];
                $data[]=$v;
            } 
        }
        $data=$this->paixu("index",$data);
        return $data;
    }
    public function gettag(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            
            $staff_id=$params["staff_id"];
            $staff=db("crm_staff")->where(["staff_id"=>$staff_id])->find();
            if(!$staff){
                \exception("登录异常");
            }
            if($staff["is_del"])
                \exception("登录异常");
            if($staff["sys_custodian"]==1||$staff["is_dynamic_manage"]==0){
                //管理员
                $data=db("crm_subregion")->field("subregion_id as `index`,subregion_name as `name`")->select();
            }else{
                if($staff["is_dynamic"]==1&&$staff["is_downstream"]==1){
                    \exception("您没有互动社区的使用权限，请联系管理员");
                }else{
                    if($staff["is_downstream"]==1){
                        //员工
                        //所有可见
                        $allcouldsee=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_sys b","a.subregion_id=b.subregion_id")
                        ->where("b.is_staff_all_visible=0")
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //自己为管理员
                        $manage=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_manager b","a.subregion_id=b.subregion_id")
                        ->where(["staff_id"=>$staff_id])
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //指定自己可见的
                        $ownsee=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_visible_staff b","a.subregion_id=b.subregion_id")
                        ->where(["staff_id"=>$staff_id])
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //指定自己部门可见的
                        $departsee=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_visible_department b","a.subregion_id=b.subregion_id")
                        ->where(["department_id"=>$staff["department_id"]])
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //去重复合并
                        $data=$this->hebingarr($allcouldsee,$manage,$ownsee,$departsee);
                       
                    }else{
                        //下游
                        //所有可见并判断是否审核
                        if($staff["is_audit"]==1){
                            $where="b.is_downstream_all_visible=0 or b.is_downstream_all_visible=1";
                        }else{
                            $where="b.is_downstream_all_visible=0";
                        }
                        $allcouldsee=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_sys b","a.subregion_id=b.subregion_id")
                        ->where($where)
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //指定自己可见的
                        $ownsee=db("crm_subregion")->alias("a")
                        ->join("sh_crm_subregion_visible_downstream b","a.subregion_id=b.subregion_id")
                        ->where(["b.downstream_id"=>$staff_id])
                        ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        //指定自己公司可见
                        $company_id=db("client_staff_relevance")->alias("a")
                        ->join("sh_client b","a.client_id=b.client_id")
                        ->where(["a.staff_id"=>$staff_id])
                        ->column("a.client_id");
                        if($company_id){
                            $company_id=implode(',',$company_id);
                            $company=db("crm_subregion")->alias("a")
                            ->join("sh_crm_subregion_visible_downtag b","a.subregion_id=b.subregion_id")
                            ->where("`type`=2 and obj_id in ({$company_id})")
                            ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        }else{
                            $company=[];
                        }
                        //指定公司分类
                        $client_cate_id=db("client_staff_relevance")->alias("a")
                        ->join("sh_client b","a.client_id=b.client_id")
                        ->join("sh_client_category_relevance c","b.client_id=c.client_id")
                        ->group("c.client_cate_id")
                        ->where(["a.staff_id"=>$staff_id])
                        ->column("c.client_cate_id");
                        if($client_cate_id){
                            $client_cate_id=implode(',',$client_cate_id);
                            $client_cate=db("crm_subregion")->alias("a")
                            ->join("sh_crm_subregion_visible_downtag b","a.subregion_id=b.subregion_id")
                            ->where("`type`=0 and obj_id in ({$client_cate_id})")
                            ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        }else{
                            $client_cate=[];
                        }
                        //指定联系人标签
                        $tag_id=db("crm_staff_tag_relevance")->where(["staff_id"=>$staff_id])->column("tag_id");
                        if($tag_id){
                            $tag_id=implode(',',$tag_id);
                            $tag=db("crm_subregion")->alias("a")
                            ->join("sh_crm_subregion_visible_downtag b","a.subregion_id=b.subregion_id")
                            ->where("`type`=1 and obj_id in ({$tag_id})")
                            ->field("a.subregion_id as `index`,a.subregion_name as `name`")->select();
                        }else{
                            $tag=[];
                        }
                        $data=$this->hebingarr($allcouldsee,$company,$ownsee,$client_cate,$tag);

                    }
                }
            }
            

            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function gettagfull(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $staff_id=$params["staff_id"];
            $staff=db("crm_staff")->where(["staff_id"=>$staff_id])->find();
       
            $app1=db("crm_subregion_manager")
                ->where(["subregion_id"=>$subregion_id,"staff_id"=>$staff_id])
                ->find();
            if($staff["sys_custodian"]==1||$staff["is_dynamic_manage"]==0||$app1){
                $where=[];
            }else{
                if($staff["is_downstream"]==1){
                    $where="is_access in (0,1)";
                }else{
                     $where="is_access in (0,2)";
                }
            }
            
            $datas=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id])->where($where)->select();
            $data=[];
            foreach ($datas as $k => $v) {
                # code...
                $tmp["name"]=$v["classify_name"];
                $tmp["curState"]=false;
                $data[]=$tmp;
            }
            $dataz["tag1"]=$data;
            $this->successTerminate($dataz);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function newclassfy(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $classify_name=trim($params["classify_name"]);
            if(!$classify_name){
                \exception("分类名不能为空");
            }
            $count=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id])->count();
            if($count>=6){
                \exception("社区内分类最多6个");
            }
            $has=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id,"classify_name"=>$classify_name])->find();
            if($has){
                \exception("社区内已存在分类".$classify_name);
            }
            $data["classify_name"]=$classify_name;
            $data["subregion_id"]=$subregion_id;
            $data["is_access"]=0;
            db("crm_subregion_classify")->insert($data);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getclass(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $staff_id=$params["staff_id"];
           
            $datas=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id])->select();
            $data=["全部"];$tag2=[];
            foreach ($datas as $k => $v) {
                # code...
                $data[]=$v["classify_name"];
                $tag2[$k]=$v["classify_name"];
            }
            $dataz["tag1"]=$data;
            $dataz["tag2"]=$tag2;
            $this->successTerminate($dataz);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getclassify(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $staff_id=$params["staff_id"];
           
            $datas=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id])->select();
           
            $this->successTerminate($datas);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getmanager(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $sys=db("crm_staff")->where(["is_downstream"=>1,"sys_custodian"=>1])->field("staff_id,staff_name,head_img")->select();
                $member=[];
                foreach ($sys as $key => $value) {
                    # code...
                    $sys[$key]["type"]="[系统管理员]".$value["staff_name"];
                    $member[]=$sys[$key];
                }
                $app=db("crm_staff")->where(["is_downstream"=>1,"sys_custodian"=>0,"is_dynamic_manage"=>0])->field("staff_id,staff_name,head_img")->select();
                foreach ($app as $key => $value) {
                    # code...
                    $app[$key]["type"]="[应用管理员]".$value["staff_name"];
                    $member[]=$app[$key];
                }
                $app1=db("crm_staff")->alias("a")
                ->join("sh_crm_subregion_manager b","a.staff_id=b.staff_id")
                ->where(["a.is_downstream"=>1,"a.sys_custodian"=>0,"a.is_dynamic_manage"=>1,"a.is_dynamic"=>0,"b.subregion_id"=>$subregion_id])
                ->field("a.staff_id,a.staff_name,head_img")->select();
                foreach ($app1 as $key => $value) {
                    # code...
                    $app[$key]["type"]="[应用子管理员]".$value["staff_name"];
                    $member[]=$app[$key];
                }
                $data=[];
                $data["member1"]=[];
                $data["member2"]=[];
                $data["is_more"]=0;
                if(count($member)>3){
                    $data["is_more"]=1;
                    for($i=0;$i<count($member);$i++){
                        if($i<=2){
                            $data["member1"][]=$member[$i];
                        }else{
                            $data["member2"][]=$member[$i];
                        }
                    }
                }else{
                    $data["member1"]=$member;
                }
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getmessagelist(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $sys=db("crm_subregion_sys")->where(["subregion_id"=>$subregion_id])->find();
            $staff_id=$params["staff_id"];
            $staff=db("crm_staff")->where(["staff_id"=>$staff_id])->find();
            //判断是否登录人为管理员
            $manage=0;
           
            if($staff["sys_custodian"]==1||$staff["is_dynamic_manage"]==0){
                $manage=1;
            }
            $has=db("crm_subregion_manager")->where(["staff_id"=>$staff_id,"subregion_id"=>$subregion_id])->find();
            if($has){
                $manage=2;
            }
            $page = isset($params['page'])&&!empty($params['page']) ? $params['page'] : 1;
            $pageSize = isset($params['rows'])&&!empty($params['rows']) ? $params['rows'] : 20;
            $where['c.subregion_id'] = $subregion_id;
            $params['search'] =  isset($params['search'])?$params['search']:'';
            $where2['content | title | abstract'] = ['like','%'.$params['search'].'%'];
            $where2['add_time'] = ['<',date('Y-m-d H:i:s')];
            $where3=[];
            if($params["tag"]!="全部"){
                $tag=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id,"classify_name"=>$params["tag"]])->value("classify_id");
                if($tag){
                    $where3["c.classify_id"]=$tag;
                }else{
                    $where3["c.classify_id"]=-1;
                }
            }
            $where4=[];
            if($params["type"]){
                if($params["type"]==2){
                    $where4["b.is_downstream"]=1;
                }else if($params["type"]==3){
                    $where4["b.is_downstream"]=0;
                }else if($params["type"]==4){
                    $where4="d.accessory_url is not null";
                }else if($params["type"]==5){
                    $where4="b.staff_id='{$staff_id}'";
                }
            }
            $data=db("crm_dynamic")->alias("a")
            ->join("crm_staff b","a.staff_id=b.staff_id")
            ->join("crm_dynamic_classify c","a.dynamic_id=c.dynamic_id","LEFT")
            ->join("crm_dynamic_accessory d","a.dynamic_id=d.dynamic_id","LEFT")
            ->group("a.dynamic_id")
            ->where(["a.subregion_id"=>$subregion_id])
            ->where($where2)
            ->where($where3)
            ->where($where4)
            ->field("a.*,b.staff_name,b.head_img")->order('add_time','desc')
            ->select();
            foreach ($data as $k => $v) {
                $data[$k]["comment"]="";
                $data[$k]["manage"]=$manage;
                if(!$data[$k]["content_url"])
                    $data[$k]["content_url"]="";
                # code...
                //获取图片
                $data[$k]["images"]=db("crm_dynamic_img")->where(["dynamic_id"=>$v["dynamic_id"]])->select();
                //获取附件
                $data[$k]["fujian"]=db("crm_dynamic_accessory")->where(["dynamic_id"=>$v["dynamic_id"]])->select();
                //获取分类
                $data[$k]["classify"]=db("crm_dynamic_classify")->alias("a")->join("crm_subregion_classify b","a.classify_id=b.classify_id")
                ->where(["dynamic_id"=>$v["dynamic_id"]])->field("b.*")->select();
                $data[$k]["checkList"]=db("crm_dynamic_classify")->alias("a")->join("crm_subregion_classify b","a.classify_id=b.classify_id")
                ->where(["dynamic_id"=>$v["dynamic_id"]])->column("b.classify_name");
                db("crm_dynamic_browse")->insert(["staff_id"=>$staff_id,"is_downstream"=>$staff["is_downstream"],"browse_time"=>date("Y-m-d H:i:s"),"dynamic_id"=>$v["dynamic_id"]]);
                $data[$k]["liulan"]=db("crm_dynamic_browse")->alias("a")->join("crm_staff b","a.staff_id=b.staff_id")->where(["dynamic_id"=>$v["dynamic_id"]])->count();

                //点赞
                //判断下游
                if($staff["is_downstream"]==1||$sys["is_downstream_access"]==0){
                    $data[$k]["dianzan"]=db("crm_dynamic_praise")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                     ->where("dynamic_id={$v["dynamic_id"]}")
                    ->field("a.staff_id,b.staff_name")->select();
                    $data[$k]["dianzanshu"]=count($data[$k]["dianzan"]);
                    //评论
                    $data[$k]["pinglun"]=db("crm_dynamic_comment")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                    ->where(["a.dynamic_id"=>$v["dynamic_id"]])
                    ->order("a.comment_id")
                    ->field("a.comment_id,a.comment,b.staff_name")
                    ->select();
                    $data[$k]["pinglunshu"]=count($data[$k]["pinglun"]);
                }else{
                    //下游不可见
                    $data[$k]["dianzan"]=db("crm_dynamic_praise")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                    ->where("dynamic_id={$v["dynamic_id"]}")
                    ->where("b.is_downstream=1 or a.staff_id='{$staff_id}'")
                    ->field("a.staff_id,b.staff_name")->select();
                    $data[$k]["dianzanshu"]=count($data[$k]["dianzan"]);
                    $data[$k]["pinglun"]=db("crm_dynamic_comment")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                    ->where(["a.dynamic_id"=>$v["dynamic_id"]])
                    ->where("b.is_downstream=1 or a.staff_id='{$staff_id}'")
                    ->order("a.comment_id")
                    ->field("a.comment_id,a.comment,b.staff_name")
                    ->select();
                    $data[$k]["pinglunshu"]=count($data[$k]["pinglun"]);
                }
            }
             $this->successTerminate($data);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function pinglun(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
          
            $staff_id=$params["staff_id"];
            $dynamic_id=$params["dynamic_id"];
            $comment=trim($params["comment"]);
            if(!$comment){
                \exception("评论内容不能为空");
            }
            $data["staff_id"]=$staff_id;
            $data["comment"]=$comment;
            $data["pid"]=0;
            $data["dynamic_id"]=$dynamic_id;
            $comid=db("crm_dynamic_comment")->insertGetId($data);
            $backdata["staff_name"]=db("crm_staff")->where(["staff_id"=>$staff_id])->value("staff_name");
            $backdata["comment"]=$comment;
            $backdata["comment_id"]=$comid;
            $this->successTerminate($backdata);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //置顶
    public function zhiding(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
          
            $staff_id=$params["staff_id"];
            $dynamic_id=$params["dynamic_id"];
            $dynamic=db("crm_dynamic")->where(["dynamic_id"=>$dynamic_id])->find();
            if($dynamic["is_stick"]){
                $data=0;
            }else{
                $data=1;
            }
            db("crm_dynamic")->where(["dynamic_id"=>$dynamic_id])->update(["is_stick"=>$data]);
           
            $this->successTerminate($data);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //删除
    public function deletedy(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
            $where["dynamic_id"]=$params["dynamic_id"];
            db("crm_dynamic")->where($where)->delete();
            db("crm_dynamic_accessory")->where($where)->delete();
            db("crm_dynamic_browse")->where($where)->delete();
            db("crm_dynamic_classify")->where($where)->delete();
            db("crm_dynamic_comment")->where($where)->delete();
            db("crm_dynamic_goods")->where($where)->delete();
            db("crm_dynamic_img")->where($where)->delete();
            db("crm_dynamic_line")->where($where)->delete();
            db("crm_dynamic_praise")->where($where)->delete();
            $this->successTerminate($params);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //点赞
    public function dianzan(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
            $subregion_id=$params["subregion_id"];
            $staff_id=$params["staff_id"];
            $dynamic_id=$params["dynamic_id"];
            $staff=db("crm_staff")->where(["staff_id"=>$staff_id])->find();
            $has=db("crm_dynamic_praise")->where(["staff_id"=>$staff_id,"dynamic_id"=>$dynamic_id])->find();
            if($has){
                db("crm_dynamic_praise")->where(["staff_id"=>$staff_id,"dynamic_id"=>$dynamic_id])->delete();
            }else{
                db("crm_dynamic_praise")->insert(["staff_id"=>$staff_id,"dynamic_id"=>$dynamic_id,"is_downstream"=>$staff["is_downstream"]]);
            }
            $sys=db("crm_subregion_sys")->where(["subregion_id"=>$subregion_id])->find();
            if($staff["is_downstream"]==1||$sys["is_downstream_access"]==0){
                    $data["dianzan"]=db("crm_dynamic_praise")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                     ->where("dynamic_id={$dynamic_id}")
                    ->field("a.staff_id,b.staff_name")->select();
                    $data["dianzanshu"]=count($data["dianzan"]);
                }else{
                    //下游不可见
                    $data["dianzan"]=db("crm_dynamic_praise")->alias("a")
                    ->join("sh_crm_staff b","a.staff_id=b.staff_id")
                    ->where("dynamic_id={$dynamic_id}")
                    ->where("b.is_downstream=1 or a.staff_id='{$staff_id}'")
                    ->field("a.staff_id,b.staff_name")->select();
                    $data["dianzanshu"]=count($data["dianzan"]);
                    
                }
            $this->successTerminate($data);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //标签修改
    public function changetag(Request $request){
         try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
            $dynamic_id=$params["dynamic_id"];
            $classify=$params["checkList"];
            $subregion_id=$params["subregion_id"];
            $insertdata=[];
            $insertdata[]=["dynamic_id"=>$dynamic_id,"classify_id"=>0];
            foreach ($classify as $k => $v) {
                # code...
                $cid=db("crm_subregion_classify")->where(["subregion_id"=>$subregion_id,"classify_name"=>$v])->find();
                if($cid){
                    $tmp["dynamic_id"]=$dynamic_id;
                    $tmp["classify_id"]=$cid["classify_id"];
                    $insertdata[]=$tmp;
                }
            }
            db("crm_dynamic_classify")->where(["dynamic_id"=>$dynamic_id])->delete();
            db("crm_dynamic_classify")->insertAll($insertdata);
            $data=db("crm_dynamic_classify")->alias("a")->join("crm_subregion_classify b","a.classify_id=b.classify_id")
                ->where(["dynamic_id"=>$dynamic_id])->field("b.*")->select();
             $this->successTerminate($data);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function addptmessage(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->post());
            //$this->successTerminate($params);die;
            $data["staff_id"]=$params["staff_id"];
            $staff=db("crm_staff")->where($data)->find();
            $data["subregion_id"]=$params["subregion_id"];
            //验证发帖权限
            $res=$this->yanzhengfatie($data["staff_id"],$data["subregion_id"]);
            if($res=='false'){
                $name=db("crm_subregion")->where(["subregion_id"=>$data["subregion_id"]])->value($data["subregion_name"]);
                \exception("您没有[{$name}]的发布权限，请联系管理员");
            }
            $data["content"]=$params["content"];
            if(!trim($params["content"])){
                \exception("内容不能为空");
            } 
            $data["title"]=trim($params["title"]);
            if(!trim($params["title"])){
                \exception("标题不能为空");
            } 
            $data["add_time"]=date("Y-m-d H:i:s");
            $data["content_url"]=$params["content_url"];
            if($data["content_url"]){
                $data["content_url"]="https://".$data["content_url"];
            }
            $data["abstract"]=$params["abstract"];
            if($params["is_share"]){
                $data["is_share"]=1;
            }else{
                $data["is_share"]=0;
            }
            $data["is_stick"]=$params["is_stick"]?1:0;
            $data["is_issue"]=!$params["is_issue"]?1:0;
            $visible_type=$params["visible_type"];
            if($visible_type=="未设置"){
                \exception("未设置可见范围");
            }else if($visible_type!="公开"){
                if(!$params["user"]){
                    \exception("未设置可见成员或不可见成员");
                }
            }
            $classify=$params["classify"];
            $classifys=[];
            if($classify){
                $classify=explode(",",$classify);
                foreach ($classify as $k => $v) {
                    # code...
                    $tmp["classify_id"]=db("crm_subregion_classify")->where(["subregion_id"=>$data["subregion_id"],"classify_name"=>$v])->value("classify_id");
                    $classifys[]=$tmp;
                }
            }
            $dynamic_id=db("crm_dynamic")->insertGetId($data);

            if($visible_type=="公开"){
                $vidata["dynamic_id"]=$dynamic_id;
                $vidata["visible_type"]=1;
                if($staff["is_downstream"]==1){
                    $vidata["is_downstream"]=0;
                }else{
                    $vidata["is_downstream"]=1;
                }
                $vidata["add_time"]=$data["add_time"];
                db("crm_dynamic_line")->insert($vidata);
            }else if($visible_type=="部分可见"){
                $vidatas=[];
                foreach ($params["user"] as $k => $v) {
                    # code...
                    $tmpk["dynamic_id"]=$dynamic_id;
                    $tmpk["staff_id"]=$v["staff_id"];
                    $tmpk["visible_type"]=2;
                    if($staff["is_downstream"]==1){
                        $tmpk["is_downstream"]=0;
                    }else{
                        $tmpk["is_downstream"]=1;
                    }
                    $tmpk["add_time"]=$data["add_time"];
                    $vidatas[]=$tmpk;
                }
                db("crm_dynamic_line")->insertAll($vidatas);
            }else{
                $vidatas=[];
                foreach ($params["user"] as $k => $v) {
                    # code...
                    $tmpk["dynamic_id"]=$dynamic_id;
                    $tmpk["staff_id"]=$v["staff_id"];
                    $tmpk["visible_type"]=3;
                    if($staff["is_downstream"]==1){
                        $tmpk["is_downstream"]=0;
                    }else{
                        $tmpk["is_downstream"]=1;
                    }
                    $tmpk["add_time"]=$data["add_time"];
                    $vidatas[]=$tmpk;
                }
                db("crm_dynamic_line")->insertAll($vidatas);
            }
            if($classifys){
                foreach ($classifys as $k => $v) {
                    # code...
                    $classifys[$k]["dynamic_id"]=$dynamic_id;
                }
                db("crm_dynamic_classify")->insertAll($classifys);
            }else{
                db("crm_dynamic_classify")->insert(["dynamic_id"=>$dynamic_id,"classify_id"=>0]);
            }
            if(isset($params["images"])){
                if($params["images"]){
                    $images=[];
                    foreach ($params["images"] as $k => $v) {
                        # code...
                        $tmps["dynamic_id"]=$dynamic_id;
                        $tmps["img_url"]=$v["url"];
                        $tmps["img_name"]=$v["name"];
                        $images[]=$tmps;
                    }
                    db("crm_dynamic_img")->insertAll($images);
                }
                
            }
            if(isset($params["fileList"])){
                if($params["fileList"]){
                    $images2=[];
                    foreach ($params["fileList"] as $k => $v) {
                        # code...
                        $tmps2["dynamic_id"]=$dynamic_id;
                        $tmps2["accessory_url"]=explode('|',$v["_t"])[0];
                        $tmps2["accessory_name"]=$v["name"];
                        $images2[]=$tmps2;
                    }
                    db("crm_dynamic_accessory")->insertAll($images2);
                }
            }
            $this->successTerminate($params);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //验证是否拥有发帖权限
    public function yanzhengfatie($staff_id,$subregion_id){
        $StaffModel = new \app\crm\model\Staff();
        $data["staff_id"]=$staff_id;
        $staff=db("crm_staff")->where($data)->find();
        $sub=db("crm_subregion_sys")->where(["subregion_id"=>$subregion_id])->find();
        if($staff["is_downstream"]==0){
            //下游
            if($sub["is_downstream_issue"]){
                return 'true';
            }else{
                return 'false';
            }
        }else{
            //员工
            if($sub["is_staff_issue"]){
                return 'true';
            }else{
                //管理员
                if($staff["sys_custodian"]==1||$staff["is_dynamic_manage"]==0){
                    return 'true';
                }
                //频道管理员
                $app1=db("crm_staff")->alias("a")
                ->join("sh_crm_subregion_manager b","a.staff_id=b.staff_id")
                ->where(["b.subregion_id"=>$subregion_id,"a.staff_id"=>$staff_id])
                ->field("a.staff_id,a.staff_name,head_img")->find();
               if($app1){
                    return 'true';
               }
               $user2=db("crm_subregion_issue_staff")->where(["subregion_id"=>$subregion_id,"staff_id"=>$staff_id])->find();
               $dep2=db("crm_subregion_issue_department")->where(["subregion_id"=>$subregion_id,"department_id"=>$staff["department_id"]])->find();
               if($user2||$dep2){
                return 'true';
               }else{
                return 'false';
               }
            }
           
          
        }
    }
    public function getfullmanager(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $params = delToken($request->get());
            $subregion_id=$params["subregion_id"];
            $sys=db("crm_staff")->where(["is_downstream"=>1,"sys_custodian"=>1])->field("staff_id,staff_name,head_img")->select();
                $member=[];
                foreach ($sys as $key => $value) {
                    # code...
                    $sys[$key]["type"]="[系统管理员]";
                    $member[]=$sys[$key];
                }
                $app=db("crm_staff")->where(["is_downstream"=>1,"sys_custodian"=>0,"is_dynamic_manage"=>0])->field("staff_id,staff_name,head_img")->select();
                foreach ($app as $key => $value) {
                    # code...
                    $app[$key]["type"]="[应用管理员]";
                    $member[]=$app[$key];
                }
                $app1=db("crm_staff")->alias("a")
                ->join("sh_crm_subregion_manager b","a.staff_id=b.staff_id")
                ->where(["a.is_downstream"=>1,"a.sys_custodian"=>0,"a.is_dynamic_manage"=>1,"a.is_dynamic"=>0,"b.subregion_id"=>$subregion_id])
                ->field("a.staff_id,a.staff_name,head_img")->select();
                foreach ($app1 as $key => $value) {
                    # code...
                    $app[$key]["type"]="[应用子管理员]";
                    $member[]=$app[$key];
                }
                $data=[];

                $data=$member;
                
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //获取分社区联系人
    public function getlinkman($subregion_id){
        $StaffModel = new \app\crm\model\Staff();
        $user=db("crm_subregion_linkman_staff")->where(["subregion_id"=>$subregion_id])->column("staff_id");
        $where1="";
        if($user){
            foreach ($user as $k => $v) {
                # code...
                $user[$k]="'".$v."'";
            }
            $ustr=implode(',',$user);
            $where1="staff_id in ($ustr)";
        }
        $depart=db("crm_subregion_linkman_department")->where(["subregion_id"=>$subregion_id])->column("department_id");
         $where2="";
        if($depart){
            foreach ($depart as $k => $v) {
                # code...
                $depart[$k]="'".$v."'";
            }
            $dstr=implode(',',$depart);
            $where2="department_id in ($dstr)";
        }
        $list=db("crm_staff")->where($where1)->whereOr($where2)->field("staff_id,staff_name,phone,email,special_plane,qq,wechat")->select();
        echo json_encode($list);
    }
     public function clientlevel(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $data=db("client_level")->where("is_delete=0")->column("level_name");
                
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function getpro(Request $request){
        try {
            $StaffModel = new \app\crm\model\Staff();
            $data=db("goods")->where("is_putaway=1")->field("goods_name,goods_sn")->select();
                
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}
    